#ifndef GENERAL_CONFIG_H
#define GENERAL_CONFIG_H
#include "gd32f10x.h"
#include "systick.h"
#include "key.h"
#include "lcd_app.h"
#include "app.h"

#define FLASH_DATA_FLAG_ADDR 0x08003000
#define FLASH_VERSION_ADDR 0x08003004
#define FLASH_DATA_FLAG 0x1111

#define PUMP1_PIN           GPIO_PIN_5  //气泵1
#define PUMP1_GPIO_PORT     GPIOE
//#define PUMP1_GPIO_CLK                    RCU_GPIOE

// #define PUMP2_PIN           GPIO_PIN_4  //气泵2
// #define PUMP2_GPIO_PORT     GPIOE
// //#define PUMP2_GPIO_CLK                    RCU_GPIOE

#define MOTOR1_PIN          GPIO_PIN_3  //电机1
#define MOTOR1_GPIO_PORT    GPIOE
//#define MOTOR1_GPIO_CLK                    RCU_GPIOE

// #define MOTOR2_PIN         GPIO_PIN_2   //电机2
// #define MOTOR2_GPIO_PORT   GPIOE
// //#define MOTOR2_GPIO_CLK                    RCU_GPIOE


//红色报警灯
#define Red_alarm_light_Ture() gpio_bit_reset(GPIOB, GPIO_PIN_8);      //红色报警灯置0开启
#define Red_alarm_light_False() gpio_bit_set(GPIOB, GPIO_PIN_8);   //红色报警灯电平置1关闭

//蓝色锁屏灯
#define LedOn() gpio_bit_reset(GPIOB, GPIO_PIN_14);       //指示灯电平置0开启
#define LedOff() gpio_bit_set(GPIOB, GPIO_PIN_14);    //指示灯电平置1关闭

//绿灯
#define green_led_Ture() gpio_bit_reset(GPIOB, GPIO_PIN_15);       //指示灯电平置0开启
#define green_led_False() gpio_bit_set(GPIOB, GPIO_PIN_15);    //指示灯电平置1关闭

//模式
#define MODE_STATIC 0  //静态模式   气压10000
#define MODE_NURSE 1   //护理模式   气压最大18000    20000 
#define MODE_SIT 2	   // MODE_SIT必须是模式按键最大值
#define MODE_WAVE 0x10 //波动模式

// //翻身方式
// #define TURN_OFF 0
// #define TURN_AUTO 1
// #define TURN_LEFT 2
// #define TURN_RIGHT 3 // TURN_RIGHT必须是翻身按键最大值

//微动开关位置
#define limit1 1
#define limit2 2
#define limit3 3
#define limit4 4
//#define limit5 5
//#define limit6 6
//#define limit7 7
//#define limit8 8

//跳转页面
#define page_hight 1	//身高模式页面
#define page_weight 2	//体重模式页面
#define page_wave 3		//波动模式页面
#define page_otherturn 4//模式页面
#define page_mode 5		//其他模式页面
#define page_lock 6		//锁屏页面
#define page_home 7		//主页面
#define page_autoturn 8 //模式页面
#define page_low_pressure 9 //压力过低弹窗页面

//断电检测引脚
#define POWER_DETECT_GPIO   GPIOC
#define POWER_DETECT_PIN    GPIO_PIN_2

//控制超级电容给蜂鸣器模块供电引脚
#define SCAP_Control_PIN    GPIO_PIN_5

extern volatile uint8_t warn_button;//报警状态，2运行，4取消

extern volatile uint8_t warn_state;//低压报警，0不报警，1第一种报警，2第二种报警

extern volatile uint8_t Beep_one_flg;//蜂鸣器只运行一次标志位

extern uint8_t RED_FIG;
extern volatile uint8_t Red_Flg_test1;//红灯闪烁标志位

extern volatile uint8_t Yellow_Flg_test1; // 黄灯闪烁标志位

extern bool system_started;

extern volatile uint8_t Beep_one_flg_1;
extern volatile uint8_t Beepstop_Flg;//蜂鸣器停止标志位

extern volatile uint8_t  Alarm_type;

extern volatile uint8_t Charge_Complete_Flag;  // 0：未完成，1：已完成

// extern static uint16_t Warning_time_s;

// typedef struct
//{
// uint8_t hight;
// uint8_t weight;
// uint8_t	TurnCycle_lenth;
// uint8_t	WaveCycle_lenth;
// uint8_t turn;
// uint8_t mode;
//
// uint8_t blinkOption;
// uint8_t blinkFlag;
// uint16_t blinkCnt;
// uint16_t blinkOption_time;
// }settingflag;
typedef struct
{
	uint8_t head_gravity;//头部重力
	uint8_t should_gravity;
	uint8_t haunch_gravity;
	uint8_t foot_gravity;

	uint8_t LowPress_ON;
	uint16_t LowPress_time;
} warn_para_t;

typedef struct
{
	uint8_t PressSensOn; //压力传感插入

	uint8_t PageNO; //当前页数
	uint8_t old_pageNO;

	uint8_t timer_1s;
	uint8_t route_conected; //路由器连接
	uint8_t mqtt_conected;	// mqtt连接
	uint8_t KeyScanFlag;
	uint8_t IsLocked;	  //锁键标志位

	uint8_t BeepKey_off;	  // 重置蜂鸣器关闭按键次数

	uint8_t wifiKey_No;	  // wifi重置按键次数
	uint8_t wifiReset;	  // wifi重置标志
	uint8_t set_password; // 1，配网状态；2，配网成功

	uint8_t SendFWInfo; //发送版本号等信息标志位

	uint8_t KeepUpKey;
	uint8_t KeepDownKey;
	uint8_t SendDataOn; //时间校准后才发业务数据，否则服务器出错
	uint16_t KeyKeepCnt;

	int16_t LockCnt;	   //锁键时间
	uint16_t wifiKey_time; // wifi按键间隔时间
	uint16_t BeepKey_time; // 蜂鸣器按键间隔时间
} flag_t;

typedef struct
{
	uint8_t bit0 : 1;
	uint8_t bit1 : 1;
	uint8_t bit2 : 1;
	uint8_t bit3 : 1;
	uint8_t bit4 : 1;
	uint8_t bit5 : 1;
	uint8_t bit6 : 1;
	uint8_t bit7 : 1;
} BYTE_t;

// enum MattressMode
//{
//	STATIC 	=0,
//	WAVE 		=1,
//	NURSE		=2,
//	SIT			=3,
//	TURN_LEFT=4,
//	TURN_RIGHT=5,
//	TURN_AUTO =6,
//
// };
typedef struct
{
	uint8_t powON;	//开关
	uint8_t hight;	// cm
	uint8_t weight; // kg
	//	enum	MattressMode mode;						//静态 0x00,交替 0x01
	uint8_t mode;
	uint8_t WaveCycle_lenth; //交替周期 分钟
	uint8_t turn_mode;		 //翻身方式
	uint8_t TurnCycle_lenth; //翻身周期 分钟
	uint8_t turn_side;		 //状态1:左翻 2：右翻 0：不翻身
	uint8_t air_column;		 //当前气柱 A 0X01 B 0X02 C 0X03(两根都充气)
	uint8_t warn_info;		 //位0置1：未充盈 ，位1 ：低压
	uint8_t key_option;		 //当前处于那个项目的设置
	//	uint16_t left_time;									//剩余交替时间 秒
	uint8_t inbed;			 //在离床
	uint8_t body_moving;	 //体动
	uint16_t air_pressure;	 //气压值
	uint8_t turn_status;   //翻身充气状态

	uint8_t Shoulder_Yellow_Pressure_Threshold; //肩部黄色预警压力阈值
	uint8_t Shoulder_Yellow_OverTime_Threshold; //肩部黄色预警时间阈值
	uint8_t Shoulder_Red_Pressure_Threshold;	//肩部红色预警压力阈值
	uint8_t Shoulder_Red_OverTime_Threshold;	//肩部红色预警时间阈值

	uint8_t Back_Yellow_Pressure_Threshold; //背部黄色预警压力阈值
	uint8_t Back_Yellow_OverTime_Threshold; //背部黄色预警时间阈值
	uint8_t Back_Red_Pressure_Threshold;	//背部红色预警压力阈值
	uint8_t Back_Red_OverTime_Threshold;	//背部红色预警时间阈值

	uint8_t Hip_Yellow_Pressure_Threshold; //臀部黄色预警压力阈值
	uint8_t Hip_Yellow_OverTime_Threshold; //臀部黄色预警时间阈值
	uint8_t Hip_Red_Pressure_Threshold;	   //臀部红色预警压力阈值
	uint8_t Hip_Red_OverTime_Threshold;	   //臀部红色预警时间阈值

	uint8_t Leg_Yellow_Pressure_Threshold; //腿部黄色预警压力阈值
	uint8_t Leg_Yellow_OverTime_Threshold; //腿部黄色预警时间阈值
	uint8_t Leg_Red_Pressure_Threshold;	   //腿部红色预警压力阈值
	uint8_t Leg_Red_OverTime_Threshold;	   //腿部红色预警时间阈值

} set_para_t;

extern flag_t Flag;
extern BYTE_t PumpStatus;
extern uint8_t cnt_shake_sw;
extern set_para_t SettingPARA, CurrentPARA;
extern double threshold;
// extern  settingflag SettingFlag;

void debug_adc(void);
extern uint16_t debug_PowerCheck, debug_AirPress2;
extern int16_t debug_humidity;
extern warn_para_t warn_para;
extern volatile uint8_t Power_stop_Flg;
extern volatile uint8_t warn_state_flg; // 低压报警，0不报警，1第一种报警，2第二种报警
#endif
